package com.example.androidmsg.seiveces;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.example.androidmsg.MainActivity;
import com.example.androidmsg.R;
import com.example.lib.socket.MyClientSocket;

public class ForeService extends Service {
    public static final String TAG = "ForeService";
    MyClientSocket cSocket;
    private MyBinder mBinder = new MyBinder();

   public class MyBinder extends Binder {
       public void startThread(MainActivity ma){
           String str = ma.et2.getText().toString();
           int num = Integer.parseInt(str);
           if (cSocket==null){
               cSocket=new MyClientSocket(ma.et1.getText().toString(),num);
               cSocket.call=ma.chandler;
               cSocket.DCIM=ma.DCIM;
               cSocket.start();
               ma.bu0.setText("停止服务");

           }else{
               cSocket.MyStop();
               cSocket=null;
               ma.bu0.setText("开启服务");

           }
       }
       public void sendData(MainActivity ma){
           if (cSocket!=null){
               if (cSocket.status==1){
                   cSocket.data="1";
               }else if (cSocket.status==0) {
                   Toast.makeText(ma, "未连接", Toast.LENGTH_SHORT).show();
               }else{
                   cSocket.data="1";
               }
           }else{
               Toast.makeText(ma, "未连接", Toast.LENGTH_SHORT).show();

           }

       }
        public void startDownload() {
            Log.d("test","test1");
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @SuppressLint("ForegroundServiceType")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()).setAutoCancel(true);// 点击后让通知将消失
        Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentText("服务启动");
        mBuilder.setContentTitle("开始服务");
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setWhen(System.currentTimeMillis());//通知产生的时间，会在通知信息里显示
        mBuilder.setPriority(Notification.PRIORITY_DEFAULT);//设置该通知优先级
        mBuilder.setOngoing(false);//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
        mBuilder.setDefaults(Notification.DEFAULT_ALL);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String channelId = "channelId" + System.currentTimeMillis();
            NotificationChannel channel = new NotificationChannel(channelId, getResources().getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId);
        }
        mBuilder.setContentIntent(pi);
        startForeground(1, mBuilder.build());


        Log.d(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onCreate() {
        super.onCreate();


        Log.d(TAG, "onCreate() executed");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
        Log.d("test","onDestroy");
    }



}
